home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1999 May / Cd Pc Users extra 20 mayo 1999.iso / Prog / Inst / FTP / FILES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-22  |  12.2 KB  |  375 lines

  1. /* files.c */
  2.  
  3. #include <windows.h>
  4. #include "fce.h"
  5. #include "files.h"
  6. #include "info.h"
  7. #include "message.h"
  8.  
  9. extern HWND hMainWnd;            /* main window handle */
  10. extern HWND hInfoWnd;            /* popup handle */ 
  11. extern HCURSOR ArrowCursor;      /* arrow cursor */
  12. extern HCURSOR WaitCursor;
  13. extern int ConnectStatus;
  14.  
  15. #define POST_MSG(m) PostMessage(hDlgWnd,WM_USER,(m),0)
  16.  
  17. #define USE_FCE_DRIVER 
  18.  
  19. #define MAX_BUF 5000
  20. #define MAX_STR 64
  21.  
  22. /* private */
  23.  
  24. static HWND hDlgWnd;
  25. static int  NextState = STATE_IDLE;
  26. static int  BinaryMode = FALSE;
  27. static char DataBuffer[MAX_BUF];
  28. static char ServerDir[MAX_STR];
  29. static char LocalDir[MAX_STR];
  30. static char FileName[MAX_STR];
  31. static char Temp[MAX_STR];
  32.  
  33. void Message(LPSTR);
  34. int AskUser(LPSTR);
  35. void ShowError(int Code);
  36. int IsConnected(void);
  37.  
  38. void SetStatusText(HWND hDlg, LPSTR Text)
  39. {
  40.  SetDlgItemText(hDlg, ID_STAT_BOX, Text);
  41. }
  42.  
  43. void AddStatusText(HWND hDlg, LPSTR Text)
  44. {int Len;
  45.  Len = GetDlgItemText(hDlg, ID_STAT_BOX, (LPSTR)DataBuffer, MAX_BUF-MAX_STR);
  46.  if(Len>0)
  47.    {DataBuffer[Len] = '\r';
  48.     DataBuffer[Len+1] = '\n';
  49.     lstrcpy((LPSTR)&DataBuffer[Len+2],Text);
  50.     SetDlgItemText(hDlg, ID_STAT_BOX, (LPSTR)DataBuffer);
  51.    }
  52. }
  53.  
  54. void EnableStopButton(HWND hDlg, int EnableFlag)
  55. {int i;
  56.  EnableWindow(GetDlgItem(hDlg,ID_STOP_BTN), EnableFlag);
  57.  for(i=ID_EXIT_BTN;i<=ID_DEL_BTN;i++) 
  58.     EnableWindow(GetDlgItem(hDlg,i), !EnableFlag);
  59.  
  60. #ifdef WIN32
  61. BOOL CALLBACK
  62. #else
  63. BOOL FAR PASCAL
  64. #endif
  65. FilesDlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
  66. {int i, Code;
  67.  static long ByteCount;
  68.  static long LastCount;
  69.  switch (iMsg)
  70.    {case WM_INITDIALOG:
  71.       hDlgWnd = hDlg;
  72.       if(IsConnected()) 
  73.         {/* show local directory */        
  74.          Code = fceGetLocalDir(0, (LPSTR)LocalDir, MAX_STR);    
  75.          if(Code<0) ShowError(Code);
  76.          else
  77.            {LocalDir[Code] = '\0';
  78.             SetDlgItemText(hDlg, ID_LDIR_BOX, (LPSTR)LocalDir);
  79.             wsprintf((LPSTR)Temp,"Local directory is %s", (LPSTR)LocalDir);
  80.             SetStatusText(hDlg,(LPSTR)Temp);
  81.            }          
  82.          /* get server directory */
  83.          fceGetServerDir(0,(LPSTR)ServerDir, MAX_STR);
  84.          SetDlgItemText(hDlg, ID_SDIR_BOX, (LPSTR)ServerDir);
  85.          wsprintf((LPSTR)Temp,"Server directory is %s", (LPSTR)ServerDir);
  86.          AddStatusText(hDlg,(LPSTR)Temp); 
  87.          /* set BINARY mode */
  88.          Code = fceSetMode(0,'B');
  89.          if(Code<0) ShowError(Code);
  90.          else
  91.            {BinaryMode = TRUE;
  92.             CheckRadioButton(hDlg, ID_ASC_RB, ID_BIN_RB, ID_BIN_RB);
  93.             SetStatusText(hDlg,"BINARY mode set");
  94.            }
  95.          SetTimer(hDlgWnd,0,50,NULL);
  96.         }
  97.       else EndDialog(hDlg, 0);
  98.       /* disable STOP button */
  99.       EnableStopButton(hDlg, 0);
  100.       return TRUE;
  101.  
  102.     case WM_TIMER:
  103.       switch(NextState)
  104.         {case STATE_IDLE:
  105.            return TRUE;
  106.          case STATE_UPLOAD:
  107.            NextState = STATE_IDLE;
  108.            for(i=1;i<=5;i++)
  109.               {Code = fceDriver(0);
  110.                if(Code<0) 
  111.                   {ShowError(Code);
  112.                    NextState = STATE_CLEANUP;
  113.                    return TRUE;
  114.                   } 
  115.                if(Code==0) 
  116.                  {ByteCount = fceGetInteger(0, FCE_GET_FILE_BYTES_SENT);
  117.                   InfoFileMsg(ByteCount);
  118.                   wsprintf((LPSTR)Temp,"%s uploaded (%ld bytes)",
  119.                                     (LPSTR)FileName, ByteCount);
  120.                   AddStatusText(hDlg,(LPSTR)Temp);   
  121.                   NextState = STATE_CLEANUP;
  122.                   return TRUE;
  123.                  }
  124.               }
  125.            /* display progress */
  126.            ByteCount = fceGetInteger(0, FCE_GET_FILE_BYTES_SENT);
  127.            if(ByteCount!=LastCount) 
  128.              {InfoFileMsg(ByteCount);
  129.               ShowWindow(hInfoWnd,SW_SHOW);
  130.               SendMessage(hInfoWnd,WM_USER,0,0L);
  131.               LastCount = ByteCount;
  132.              }               
  133.            NextState = STATE_UPLOAD;  
  134.            break;
  135.            
  136.          case STATE_DNLOAD:
  137.            NextState = STATE_IDLE;
  138.            for(i=1;i<=7;i++)
  139.              {Code = fceDriver(0);
  140.                if(Code<0) 
  141.                   {ShowError(Code);
  142.                    NextState = STATE_CLEANUP;
  143.                    return TRUE;
  144.                   } 
  145.                if(Code==0) 
  146.                  {NextState = STATE_CLEANUP; 
  147.                   ByteCount = fceGetInteger(0, FCE_GET_FILE_BYTES_RCVD);
  148.                   InfoFileMsg(ByteCount);
  149.                   wsprintf((LPSTR)Temp,"%s downloaded (%ld bytes)",
  150.                                      (LPSTR)FileName, ByteCount);
  151.                   AddStatusText(hDlg,(LPSTR)Temp);
  152.                   NextState = STATE_CLEANUP;
  153.                   return TRUE;
  154.                  }       
  155.               }
  156.           /* display progress */
  157.           ByteCount = fceGetInteger(0, FCE_GET_FILE_BYTES_RCVD);
  158.           if(ByteCount!=LastCount) 
  159.             {InfoFileMsg(ByteCount);
  160.              ShowWindow(hInfoWnd,SW_SHOW);
  161.              SendMessage(hInfoWnd,WM_USER,0,0L);
  162.              LastCount = ByteCount;
  163.             }              
  164.           NextState = STATE_DNLOAD;
  165.           break;
  166.           
  167.         case STATE_CLEANUP:
  168.           NextState = STATE_IDLE;
  169.           /* re-enable automatic call to seeDriver */
  170.           fceSetInteger(0,FCE_SET_AUTO_CALL_DRIVER,1);
  171.           /* drop info window */
  172.           ShowWindow(hInfoWnd,SW_HIDE);
  173.           SetCursor(ArrowCursor);  
  174.           /* disable STOP button  */
  175.           EnableStopButton(hDlg, 0);
  176.           break;
  177.         default:
  178.           NextState = STATE_IDLE;
  179.        } /* end-switch */
  180.       return TRUE;
  181.        
  182.     case WM_COMMAND:
  183.       switch (LOWORD(wParam))
  184.         {case ID_EXIT_BTN:
  185.            if(AskUser("Log off server?")!=IDOK) return TRUE;
  186.            if(!IsConnected()) return TRUE;
  187.            ConnectStatus = FALSE;
  188.            fceClose(0);
  189.            KillTimer(hDlgWnd,0);
  190.            EndDialog(hDlg, 0);
  191.            break;
  192.            
  193.         case ID_STOP_BTN:
  194.            NextState = STATE_IDLE;
  195.            fceAbort(0);
  196.            NextState = STATE_CLEANUP;
  197.            Message("Aborted by User!");
  198.            /* disable STOP button */
  199.            EnableStopButton(hDlg, 0);
  200.            break;
  201.            
  202.         case ID_FULL_BTN:
  203.            /* get full directory list */
  204.            if(!IsConnected()) 
  205.              {EndDialog(hDlg, 0);
  206.               break;
  207.              }
  208.            SetCursor(WaitCursor);
  209.            /* change to ASCII mode */
  210.            if(BinaryMode) fceSetMode(0,'A');
  211.            /* get full file list */
  212.            Code = fceGetList(0,FCE_FULL_LIST,(LPSTR)DataBuffer,MAX_BUF);
  213.            if(Code>=0) SetStatusText(hDlg, (LPSTR)DataBuffer);
  214.            if(BinaryMode) fceSetMode(0,'B');
  215.            SetCursor(ArrowCursor);
  216.            break;
  217.            
  218.          case ID_NAME_BTN:
  219.            /* get directory name list */
  220.            if(!IsConnected()) 
  221.              {EndDialog(hDlg, 0);
  222.               break;
  223.              }
  224.            SetCursor(WaitCursor);
  225.            /* change to ASCII mode */
  226.            if(BinaryMode) fceSetMode(0,'A');
  227.            /* get filename list */
  228.            Code = fceGetList(0,FCE_NAME_LIST,(LPSTR)DataBuffer,MAX_BUF);
  229.            if(Code>=0) SetStatusText(hDlg, (LPSTR)DataBuffer);
  230.            if(BinaryMode) fceSetMode(0,'B');
  231.            SetCursor(ArrowCursor);
  232.            break;   
  233.            
  234.          case ID_ASC_RB:
  235.            /* set ASCII mode */
  236.            if(!IsConnected()) 
  237.              {EndDialog(hDlg, 0);
  238.               break;
  239.              }          
  240.            Code = fceSetMode(0,'A');
  241.            if(Code<0) ShowError(Code);
  242.            else 
  243.              {BinaryMode = FALSE;
  244.               CheckRadioButton(hDlg, ID_ASC_RB, ID_BIN_RB, ID_ASC_RB);
  245.               AddStatusText(hDlg,"ASCII mode set");
  246.              } 
  247.            break;
  248.            
  249.          case ID_BIN_RB:
  250.            /* set BINARY mode */
  251.            if(!IsConnected()) 
  252.              {EndDialog(hDlg, 0);
  253.               break;
  254.              }          
  255.            Code = fceSetMode(0,'B');
  256.            if(Code<0) ShowError(Code);
  257.            else 
  258.              {BinaryMode = TRUE;
  259.               CheckRadioButton(hDlg, ID_ASC_RB, ID_BIN_RB, ID_BIN_RB);
  260.               AddStatusText(hDlg,"BINARY mode set");
  261.              }            
  262.            break;
  263.            
  264.          case ID_SDIR_BTN:
  265.            if(!IsConnected()) 
  266.              {EndDialog(hDlg, 0);
  267.               return TRUE;
  268.              }         
  269.            /* set server directory */
  270.            if(GetDlgItemText(hDlg, ID_SDIR_BOX, (LPSTR)ServerDir, MAX_STR)==0)
  271.              Message("Missing directory name");
  272.            else
  273.              {Code = fceSetServerDir(0,(LPSTR)ServerDir);
  274.               if(Code<0) ShowError(Code);
  275.               fceGetServerDir(0,(LPSTR)ServerDir, MAX_STR);
  276.               wsprintf((LPSTR)Temp,"Server directory is %s",(LPSTR)ServerDir);
  277.               AddStatusText(hDlg,(LPSTR)Temp);
  278.               SetDlgItemText(hDlg, ID_SDIR_BOX, (LPSTR)ServerDir);                
  279.              }
  280.            break;
  281.            
  282.          case ID_LDIR_BTN:
  283.            if(!IsConnected()) 
  284.              {EndDialog(hDlg, 0);
  285.               break;
  286.              }         
  287.            /* set local directory */
  288.            if(GetDlgItemText(hDlg, ID_LDIR_BOX, (LPSTR)LocalDir, MAX_STR)==0)
  289.              Message("Missing directory name");
  290.            else
  291.              {Code = fceSetLocalDir(0,(LPSTR)LocalDir);
  292.               if(Code<0) ShowError(Code);
  293.               else 
  294.                 {wsprintf((LPSTR)Temp,"Local directory is %s",(LPSTR)LocalDir);
  295.                  AddStatusText(hDlg,(LPSTR)Temp);
  296.                 }              
  297.              }
  298.            break; 
  299.            
  300.          case ID_GET_BTN:
  301.            if(!IsConnected()) 
  302.              {EndDialog(hDlg, 0);
  303.               break;
  304.              }  
  305.            /* download file */
  306.            if(GetDlgItemText(hDlg, ID_FILE_BOX, (LPSTR)FileName, MAX_STR)==0)
  307.              Message("Missing file name");
  308.            else
  309.              {/* enable STOP button */
  310.               EnableStopButton(hDlg, 1);
  311.               /* download the file */
  312.               SetCursor(WaitCursor);
  313.               /* disable AUTO */
  314.               fceSetInteger(0,FCE_SET_AUTO_CALL_DRIVER,0);
  315.               Code = fceGetFile(0,(LPSTR)FileName);            
  316.               if(Code<0) ShowError(Code);
  317.               else NextState = STATE_DNLOAD;
  318.              }
  319.            break; 
  320.            
  321.          case ID_PUT_BTN:
  322.            if(!IsConnected()) 
  323.              {EndDialog(hDlg, 0);
  324.               break;
  325.              } 
  326.            /* upload file */
  327.            if(GetDlgItemText(hDlg, ID_FILE_BOX, (LPSTR)FileName, MAX_STR)==0)
  328.              Message("Missing file name");
  329.            else
  330.              {/* enable STOP button */
  331.               EnableStopButton(hDlg, 1);
  332.               /* upload the file */
  333.               SetCursor(WaitCursor);
  334.               /* disable AUTO */
  335.               fceSetInteger(0,FCE_SET_AUTO_CALL_DRIVER,0);
  336.               Code = fcePutFile(0,(LPSTR)FileName);
  337.               if(Code<0) ShowError(Code);
  338.               else NextState = STATE_UPLOAD;
  339.              }
  340.            break;   
  341.            
  342.          case ID_DEL_BTN:
  343.            if(!IsConnected()) 
  344.              {EndDialog(hDlg, 0);
  345.               break;
  346.              }  
  347.            /* delete file */
  348.            if(GetDlgItemText(hDlg, ID_FILE_BOX, (LPSTR)FileName, MAX_STR)==0)
  349.              Message("Missing file name");
  350.            else
  351.              {/* delete the file */
  352.               SetCursor(WaitCursor);
  353.               Code = fceDelFile(0,(LPSTR)FileName);             
  354.               SetCursor(ArrowCursor);
  355.               /* check status of deletion */
  356.               if(Code<0) ShowError(Code);
  357.               else 
  358.                 {wsprintf((LPSTR)Temp,"%s deleted",(LPSTR)FileName);
  359.                  AddStatusText(hDlg,(LPSTR)Temp);
  360.                 }
  361.              }
  362.            break;
  363.            
  364.             
  365.          default:
  366.            break;
  367.         }/* end-switch (LOWORD(wParam)) */
  368.     return TRUE;
  369.    }
  370.  return FALSE;
  371. } /* end FilesDlgProc */
  372.    
  373.  
  374.